﻿using System;
using System.Collections.Generic;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class ApplicationVu : DBFObject, IDBObject, INotifyPropertyChanged
    {

        public enum ActionIssue
        {
            ISSUE_VU,
            SUBSTITUTE_VU
        }

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }


        String m_NumberApplication = "";
        ///Номер заявления (ID)
        public String NumberApplication
        {
            get
            {
                return m_NumberApplication;
            }
            set
            {
                m_NumberApplication = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberApplication"));
            }
        }


        /// <summary>
        /// Дата создания заявления
        /// </summary>
        private DateTime? m_DateCreate = null;
        public DateTime? DateCreate
        {
            get
            {
                return m_DateCreate;
            }
            set
            {
                m_DateCreate = value;
                OnPropertyChanged(new PropertyChangedEventArgs("DateCreate"));
            }
        }


        /// <summary>
        /// Заявитель
        /// </summary>
        private int m_oOwner = 0;
        public Individual Owner
        {
            get;
            set;
        }


        /// <summary>
        /// Данные на ВУ
        /// </summary>
        private int m_VuData = 0;
        public Vu VuData
        {
            get;
            set;
        }


        /// <summary>
        /// Название ГосАвтоИнспекции
        /// </summary>
        private String mNameGosInspection = "";
        public string NameGosInspection
        {
            get
            {
                return mNameGosInspection;
            }
            set
            {
                mNameGosInspection = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NameGosInspection"));
            }
        }


        /// <summary>
        /// Причина выдачи ВУ
        /// </summary>
        private String mMotiveDelivery = "";
        public string MotiveDelivery
        {
            get
            {
                return mMotiveDelivery;
            }
            set
            {
                mMotiveDelivery = value;
                OnPropertyChanged(new PropertyChangedEventArgs("MotiveDelivery"));
            }
        }

        /// <summary>
        /// Документы от заявителя
        /// </summary>
        private String mDocuments = "";
        public string Documents
        {
            get
            {
                return mDocuments;
            }
            set
            {
                mDocuments = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Documents"));
            }
        }


        /// <summary>
        /// Документы от заявителя
        /// </summary>
        private ActionIssue mActionVu = ActionIssue.ISSUE_VU;
        public ActionIssue Action
        {
            get
            {
                return mActionVu;
            }
            set
            {
                mActionVu = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Action"));
            }
        }


        public override string ToString()
        {
            return String.Format("Заявление на ВУ №{0}", NumberApplication);
        }


        public ApplicationVu()
        {
            NameTable = "APPLICATION_VU_TABLE";            
            Owner = new Individual();
            VuData = new Vu();
            NameGosInspection = "";
            MotiveDelivery = "";
            Documents = "";
            DateCreate = DateTime.Now;
        }


        public object Save()
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("DATE_CREATE", DateCreate);
            Parameters.Add("ID_OWNER", (Owner as IDBObject).Save());
            Parameters.Add("ID_VU", (VuData as IDBObject).Save());
            Parameters.Add("NAME_GOS_INSPECTION", NameGosInspection);
            Parameters.Add("MOTIVE_DELIVERY", MotiveDelivery);
            Parameters.Add("DOCUMENTS", Documents);
            Parameters.Add("ACTION", (int)Action);

            return NumberApplication = (String)SaveObject();
        }

        public void Load(object ID)
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("DATE_CREATE", DateCreate);
            Parameters.Add("ID_OWNER", m_oOwner);
            Parameters.Add("ID_VU", m_VuData);
            Parameters.Add("NAME_GOS_INSPECTION", NameGosInspection);
            Parameters.Add("MOTIVE_DELIVERY", MotiveDelivery);
            Parameters.Add("DOCUMENTS", Documents);
            Parameters.Add("ACTION", (int)Action);
            
            LoadObject(ID);

            DateCreate = Parameters["DATE_CREATE"] == null || Parameters["DATE_CREATE"].ToString() == String.Empty
                ? (DateTime?)null
                : DateTime.Parse(Parameters["DATE_CREATE"].ToString());

            ((Owner = new Individual()) as IDBObject).Load(Parameters["ID_OWNER"]);
            ((VuData = new Vu()) as IDBObject).Load(Parameters["ID_VU"]);

            NameGosInspection = (String)Parameters["NAME_GOS_INSPECTION"];
            MotiveDelivery = (String)Parameters["MOTIVE_DELIVERY"];
            Documents = (String)Parameters["DOCUMENTS"];
            Action = (ActionIssue)Parameters["ACTION"];

            NumberApplication = (String)ID;
        }

        public void Update()
        {
            (Owner as IDBObject).Update();
            (VuData as IDBObject).Update();

            Parameters = new Dictionary<string, object>();
            Parameters.Add("DATE_CREATE", DateCreate);
            Parameters.Add("NAME_GOS_INSPECTION", NameGosInspection);
            Parameters.Add("MOTIVE_DELIVERY", MotiveDelivery);
            Parameters.Add("DOCUMENTS", Documents);
            Parameters.Add("ACTION", (int)Action);

            UpdateObject();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
